require 'IRISPoller'
require 'GooglePoller'
require 'TrendPointPoller'

class Poller < ActiveRecord::Base

  def self.poll
    # get data points from IRIS poller
    IrisPoller.new.poll_hosts.each do |dp|

      # see if point should be recorded
      if (lookup = IrisLookup.find(:first, :conditions=>{:key => dp[1], :address => dp[0]})) != nil
        dp[2] = dp[2].to_i * lookup.conversion_rate
        # create new Water or Power entry based upon lookup table
        Object::const_get(lookup.data_type.capitalize).create(:key=>"#{dp[0]}-#{dp[1]}", :time=>Time.now, :value=>dp[2])
      end
    end

    trendpoint_data = TrendPointPoller.new.poll_host
    if valid? trendpoint_data
      trendpoint_data.each do |dp|
        Power.create(:key=>dp[0], :time=>Time.now, :value=>dp[1])
      end
    end

    Weather.create(:temperature => Google.getTemperature, :conditions=> Google.getConditions)
  end

  def valid? points
    points.each do |point|
      return true if point[1] > 1
    end
    return nil
  end

end
